﻿using Common.Regexs;
using Entity.Models.dto;
using Entity.Models.Entities;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    /// <summary>
    /// 菜单管理控制器
    /// </summary>
    [CustomActionFilte]
    public class MenuInfoController : Controller
    {
        //菜单接口
        IMenuInfoBLL _menuInfoBLL;

        public MenuInfoController(IMenuInfoBLL menuInfoBLl)
        {
            _menuInfoBLL = menuInfoBLl;
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddMenuInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataMenuInfoView()
        {
            return View();
        }

        /// <summary>
        /// 获取菜单下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getMenuInfoSpinner()
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _menuInfoBLL.getMenuInfoSpinner()));
        }

        /// <summary>
        /// 获取首页菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getMenuList()
        {
            return Json(_menuInfoBLL.getMenuList(HttpContext.Session.GetString("userId")));
        }


        /// <summary>
        /// 菜单列表、搜索
        /// </summary>
        /// <param name="MenuInfoName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getMenuTreetab(string MenuInfoName)
        {
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _menuInfoBLL.getMenuTreetab(MenuInfoName)));
        }


        /// <summary>
        /// 根据菜单id搜索(修改页面数据列表和下拉框)
        /// </summary>
        /// <param name="menuInfoid">部门id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getByMenuInfo(string menuInfoid)
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), new
            {
                menuInfo = _menuInfoBLL.getByMenuInfo(menuInfoid),//菜单数据集
                menuInfoSpinner = _menuInfoBLL.getMenuInfoSpinner()//下拉菜单
            })); ;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuInfo">菜单信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddMenuInfo(MenuInfo menuInfo)
        {
            //状态信息
            string mgs;
            object data = null;
            if (Verify(menuInfo, out mgs))
            {
                try
                {
                    data = _menuInfoBLL.AddMenuInfo(menuInfo);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.SUCCESS) : mgs, data));
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>
        public IActionResult UpdataMenuInfo(MenuInfo menuInfo)
        {
            object data = null;
            string mgs = null;
            if (Verify(menuInfo, out mgs))
            {
                try
                {
                    data = _menuInfoBLL.UpdataMenuInfo(menuInfo);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            string a = string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.UPDATE) : mgs;
            return Json(new JsonResults(200, a, data));
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="menuInfoid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateMenuInfo(string menuInfoid)
        {
            object data = null;
            try
            {
                data = _menuInfoBLL.DalateMenuInfo(menuInfoid);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="menuInfos"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateMenuInfoList(List<string> menuInfos)
        {
            object data = null;
            try
            {
                data = _menuInfoBLL.DalateMenuInfoList(menuInfos);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 验证参数
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <param name="mgs"></param>
        /// <returns></returns>
        private bool Verify(MenuInfo menuInfo, out string mgs)
        {
            Regex regex = new Regex(Regular.URL);
            if (!new Regex(Regular.Name).Match(menuInfo.Title).Success)
            {
                mgs = "请输入正确的菜单名称";
                return false;
            }
            else if (menuInfo.Href != null ? !regex.Match(menuInfo.Href).Success : false)
            {
                mgs = "请输入正确的访问地址或为空（空表示作为目录）";
                return false;
            }
            else if (menuInfo.Icon != null ? !regex.Match(menuInfo.Icon).Success : false)
            {
                mgs = "请输入正确的图标样式地址";
                return false;
            }
            else if (menuInfo.Target != null ? !regex.Match(menuInfo.Target).Success : false)
            {

                mgs = "请输入正确的目标";
                return false;
            }
            else if (menuInfo.Description != null ? !new Regex(Regular.Description).Match(menuInfo.Description).Success : false)
            {
                mgs = "请输入 2-36 内的字符的描述信息";
                return false;
            }
            else if (!new Regex(Regular.Number).Match(menuInfo.Level.ToString()).Success)
            {
                mgs = "请输入正确的菜单级别";
                return false;
            }
            else if (menuInfo.Sort < 1)
            {
                mgs = "请输入正确的菜单排序";
                return false;
            }


            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
